home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln0486.arc / MENU.LT1 < prev    next >
Text File  |  1986-04-06  |  9KB  |  234 lines

  1. stty -echo; cat - > MEN.C; stty echo
  2.  
  3. #include "menu_io.c"      /* the machine specific routines */
  4. #include "men.h"       /* the header file */     
  5.  
  6.  
  7. main()    /* this is just a example 'main' */
  8. {
  9.  
  10.     men_walk(0,0,0);
  11.     exit();
  12. }
  13. test()     /* part of the example */ 
  14. {
  15.     men_curs(3,20);
  16.     men_printf("test");
  17. }
  18. one()  /* part of the example */ 
  19. {
  20.     men_curs(3,20);
  21.     men_printf("one ");
  22. }
  23. two()    /* part of the example */
  24. {
  25.     men_curs(3,20);
  26.     men_printf("two ");
  27. }
  28.  
  29.  
  30. /* the MENU library starts here */ 
  31. men_disp(n,i)
  32. int n,i;
  33. {
  34.         int i2=0;  /* helps remember where we are */
  35.  
  36.         men_curs(0,0);  /* this is relative to menu "window" */
  37.         men_el();    /* erase the top menu line */
  38.         men_curs(0,0);    /* come on home */
  39.         while (i2<men[n].noitems) {    /* loop items in current line */ 
  40.                 if (i2 == i) standout();/* highlite the indicated item */
  41.                 men_printf("%s",men[n].items[i2++]);/* print each item */
  42.                 if (i2 == i+1) standend();/* turn off highlite */
  43.                 men_printf("  ");    /* a little space betwixt items */
  44.         }
  45.         men_printf("Quit");    /* add the special "quit" item */
  46. }
  47. men_walk(n,n2,i2)
  48. int n,n2,i2;    /* n=the menu "line" number, n2=previous menu, i2=previous
  49.            highlighted item number */
  50. {
  51.         int i,ii; /* need somewhere to remember */
  52.         int FINISHED = FALSE;/* set an indicator */
  53.         int c,c2; /* for keyboard characters */
  54.         i = ii = 0; /* initialization is req'd */
  55.  
  56.         men_disp(n,i); /* put up desired menu line */
  57. while (!(FINISHED)) { /* until user quits explicitly */
  58.         menline[0] = 0; /* parameter string gets nulled */
  59.         sub_men(n,i,n2,i2); /* put up either sub-menu or help info */
  60.         men_curs(0,menlen(n,i)); /* put cursor after highlighted item */
  61.         c = men_getc(); /* get the menu selection character */   
  62.  
  63.         if (c == '/') { /* this indicates a string parameter is coming */
  64.                 menfetch(menline,LINE,LINELEN); /* so get a line of input */
  65.                 men_curs(0,menlen(n,i)); /* put cursor back*/
  66.                 c = men_getc(); /* get the menu selection character */
  67.         }
  68.         else if (isdigit(c)) { /* this indicates a numeric parameter */
  69.                 UNGET = TRUE;   /* one way to get portability */
  70.                 ungotten = c;    /* sorry,  but I've been caught on this one */
  71.                 menfetch(menline,NUMERIC,5); /* get a number */
  72.                 men_curs(0,menlen(n,i)); /* put cursor back */
  73.                 c = men_getc(); /* get the menu selection character */
  74.         }
  75.         c2 = NOSELECT; /* set up a flag to look for match */
  76.         ii = i; /* remember where we started */
  77.         if (isalpha(c)) { /* have we got a letter? */
  78.                 c2 = men_legal(n,c); /* is it one of the items? */
  79.                 if (c2 != NOSELECT) {    /* if so, lie to fake out case */
  80.                         i = c2;  /* so that the menu selection */   
  81.                         c = '\n';    /* looks like a carraige return */
  82.                 }
  83.         }
  84.         switch (c) {                    
  85.                 case ' ':  /* treat a space like a key right */
  86.                 case KEY_RIGHT: /* usually,  the right arrow key */
  87.                         unstand(n,i); /* de-select current item */
  88.                         i++; /* move to the right */
  89.                         if (i > men[n].noitems) i = 0;/* loop around, if nec */
  90.                         standout(); /* highlight new item */
  91.                         men_curs(0,menlen(n,i)); /* put the cursor*/ 
  92.                         if (i == men[n].noitems) /* if we're out of items */
  93.                                 men_printf("Quit"); /* add the quit item */
  94.                         else men_printf("%s",men[n].items[i]); /* print the item */
  95.                         standend(); /* turn off the highlight */
  96.                         men_printf("  "); /* space up */
  97.                         break; /* ta-da */
  98.                 case 'Q':  /* capital Q takes us all the way back */
  99.                         return('Q');
  100.             break;
  101.                 case 27:  /* ESC and lower-case q go back one level */
  102.                 case 'q':
  103.                         FINISHED = TRUE; /* this breaks the loop */
  104.                         break;
  105.                 case '\r': /* we'll treat these the same way */
  106.                 case '\n':
  107.                         if ( men[n].next[i] < 0)  { /* if the link < 0 */
  108.                                 /* that means we're at the bottom leaf */
  109.                 /* and need to call a function */    
  110.                                 men_curs(1,0); /* erase the bottom line */
  111.                 men_el();
  112.                 men_scp(); /* hang on to the cursor position */
  113.                 if (men[n].class[i] == DISPLAY); /* display only */ 
  114.                                 else if (men[n].class[i] == INTEGER) { 
  115.                     /* call the function with an int */ 
  116.                                         (*func[(men[n].next[i]*-1)])(atoi(menline)); 
  117.                                 }
  118.                                 else if (men[n].class[i] == STRING) {
  119.                     /*call it with a string */    
  120.                                         (*func[(men[n].next[i]*-1)])(menline); 
  121.                                 }
  122.                                 else {
  123.                     /* otherwise, just call it */ 
  124.                                         (*func[(men[n].next[i]*-1)])(); 
  125.                                 }
  126.                                 i = ii; /* go back one level */                
  127.                 men_rcp(); /* restore cursor position */    
  128.                                 break;
  129.                         }
  130.                         else {  /* its a selection but it leads to */
  131.                 /* another menu line,  so we "walk" thru */
  132.                 /* another level */
  133.                                 if (men_walk(men[n].next[i],n,i)  == 'Q') 
  134.                                         return('Q'); /* watching for an */
  135.                     /* absolute QUIT */
  136.                                 men_disp(n,i);/*re-display where we started*/
  137.                                 sub_men(n,i,n2,i2);
  138.                         }       
  139.                         break;
  140.                         
  141.                 case KEY_LEFT: /* This is just the inverse of KEY_RIGHT */
  142.                         unstand(n,i);
  143.                         if (i == 0) i=men[n].noitems;
  144.                         else i--;
  145.                         standout();
  146.                         men_curs(0,menlen(n,i));
  147.                         if (i == men[n].noitems)
  148.                                 men_printf("Quit");
  149.                         else men_printf("%s",men[n].items[i]);
  150.                         standend();
  151.                         break;
  152.  
  153.                 default: /* something wierd was entered */
  154.                         beep();
  155.                         break;
  156.         }
  157. }
  158. standend(); /* turn off any dangling stand-outs */
  159. }         /* and return to calling program */    
  160.  
  161. sub_men(n,i,n2,i2)
  162. int n,i,n2,i2; /* n,i=current line, current item, n2,i2= previous) */
  163. {
  164.         int ii = 0;             
  165.         men_curs(1,0);
  166.         men_el();
  167.  
  168.         if  (n != 0) {  /* show a little history */
  169.                 men_curs(1,COLS-10);
  170.                 men_printf("(%s)",men[n2].items[i2]);
  171.         }
  172.         men_curs(1,0);
  173.         if (men[n].noitems == i) { /* supply the "quit" help */ 
  174.                 men_printf("Quit this menu"); 
  175.         }
  176.         else if (men[n].next[i] < 0) { /* otherwise, help for selected item */
  177.                 men_printf("%s",menu_help[(men[n].next[i]*-1)]);
  178.         }
  179.         else {     /* we have to print the whole next menu line */
  180.         /* as in 'men_disp()' above */    
  181.                 men_curs(1,0);
  182.                 n = men[n].next[i];
  183.                 while (ii<men[n].noitems) {
  184.                         men_printf("%s  ",men[n].items[ii++]);
  185.                 }
  186.         }
  187. }
  188. unstand(n,i)  /* print the item i in in menu n */ 
  189. int n,i;
  190. {
  191.                 men_curs(0,menlen(n,i));
  192.                 if (i == men[n].noitems) 
  193.                         men_printf("Quit");
  194.                 else men_printf("%s",men[n].items[i]);
  195. }
  196. menlen(n,i) /* compute the length of a menu item (plust spacing interval) */
  197. int n,i;
  198. {
  199.                 int l = 0;
  200.  
  201.                 while (i--) 
  202.                         l += strlen(men[n].items[i])+2;
  203.                 return(l);
  204. }
  205. men_legal(n,c) /* check a character against a line of items */
  206. int n;
  207. char c;    /* note we're looking for upper-case */
  208. {
  209.         int i = 0;
  210.         
  211.         while (i < men[n].noitems) {
  212.                 if (men[n].items[i][0] == toupper(c)) return(i);
  213.                 i++;
  214.         }
  215.         return(NOSELECT);
  216. }
  217.  
  218. menfetch(s,ty,l) /* here's where we go when hit with slash or a number */
  219. char s[LINELEN];
  220. int ty,l;
  221. {
  222.         s[0] = 0;
  223.         men_curs(1,0);
  224.         men_el();
  225.         men_curs(1,0);
  226.         men_printf("ENTER ARGUMENT TO THE FUNCTION :");
  227.         men_curs(1,34);
  228.         sget(s,l,ty);
  229.         return; 
  230.    men_printf("ENTER ARGUMENT TO THE FUNCTION :");
  231.         men_curs(1,34);
  232.         sget(s,l,ty);
  233.         retu